import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import {LocalStorProvider} from "../../providers/local-stor/local-stor";
import {ApiProvider} from "../../providers/api/api";
import {PersonalCentrePage} from "../personal-centre/personal-centre";

/**
 * Generated class for the UpdatePasswordPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-update-password',
  templateUrl: 'update-password.html',
})
export class UpdatePasswordPage {
  password;
  newPass;
  confirmPass;
  user_id;

  constructor(public navCtrl: NavController, public navParams: NavParams,public storage:LocalStorProvider,public http:ApiProvider) {
  }

  ionViewDidLoad() {
    this.storage.getKey("user_id").then(val=>{
      this.user_id=val;
    });

  }

  /**
   * 修改密码
   */
  updatePassword()
  {
    if(this.newPass!=this.confirmPass)
    {
      this.http.showToast("新密码和确认密码不一致！","bottom");
    }
    else if(this.newPass.length<6||this.newPass.length>16)
    {
      this.http.showToast("密码长度应为6-16位！","bottom");
    }
    else
    {
      this.http.post("/userstu/updatePass.do",{user_id:this.user_id,password:this.password,newPass:this.newPass}).subscribe(item=>{
        if(item["Code"]=="11111111")
        {
          this.http.showToast(item["Message"],"bottom");
        }
        else
        {
          this.http.showToast(item["Message"],"bottom");
          this.navCtrl.push(PersonalCentrePage);
        }
      });
    }
  }
}
